using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace HeroesOfSarangone
{
    class NetworkConnection
    {
        private IPHostEntry host;
        private IPAddress[] ipAddresses;
        private EndPoint endPoint;

        private Socket socket;

        public NetworkConnection()
        {
            host = Dns.GetHostEntry("heroes.com");
            ipAddresses = host.AddressList;
            endPoint = new IPEndPoint(ipAddresses[0], 65000);

            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                socket.Connect(endPoint);
            }
            catch (SocketException e)
            {
                // Handle the exeption...
            }
        }

        public string GetData()
        {
            Byte[] reciveBytes = new Byte[256];
            Int32 bytes = this.socket.Receive(reciveBytes);

            return Encoding.ASCII.GetString(reciveBytes);
        }

        public void SendData(string str)
        {
            byte[] sendBytes = Encoding.ASCII.GetBytes(str);
            socket.Send(sendBytes, sendBytes.Length, SocketFlags.None);
        }
    }
}